<?php

/**
 * This is the model class for table "albums".
 *
 * The followings are the available columns in table 'albums':
 * @property integer $id
 * @property integer $user_id
 * @property integer $category_id
 * @property string $title
 * @property string $create_date
 * @property string $desc
 * @property string $tag
 * @property integer $status
 * @property string $thumbnail_image
 *
 * The followings are the available model relations:
 * @property Category $category
 * @property Users $user
 * @property Comments[] $comments
 * @property Images[] $images
 * @property Ratings[] $ratings
 */
class Albums extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Albums the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return 'albums';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('category_id, title', 'required'),
			array('user_id, category_id, status', 'numerical', 'integerOnly'=>true),
			array('title, tag, thumbnail_image', 'length', 'max'=>255),
            array('desc', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, user_id, category_id, title, create_date, desc, tag, status, thumbnail_image', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'category' => array(self::BELONGS_TO, 'Category', 'category_id'),
			'user' => array(self::BELONGS_TO, 'Users', 'user_id'),
			'comments' => array(self::HAS_MANY, 'Comments', 'album_id'),
			'images' => array(self::HAS_MANY, 'Images', 'album_id'),
			'ratings' => array(self::HAS_MANY, 'Ratings', 'album_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => 'ID',
			'user_id' => 'User',
			'category_id' => 'Category',
			'title' => 'Title',
			'create_date' => 'Create Date',
			'desc' => 'Desc',
			'tag' => 'Tag',
			'status' => 'Status',
			'thumbnail_image' => 'Thumbnail Image',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('user_id',$this->user_id);
		$criteria->compare('category_id',$this->category_id);
		$criteria->compare('title',$this->title,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('desc',$this->desc,true);
		$criteria->compare('tag',$this->tag,true);
		$criteria->compare('status',$this->status);
		$criteria->compare('thumbnail_image',$this->thumbnail_image,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}

    protected function beforeSave() {
        if ($this->isNewRecord) {
            $this->user_id = Yii::app()->user->id;
            $this->create_date = date('Y-m-d');
        }

        return parent::beforeSave();
    }
}